package com.example.demo.base.util;

import java.io.*;

public class FileUtil {
    public static void copy(InputStream input,OutputStream output) throws IOException {
        try {
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            input.close();
            output.close();
        }
    }
    public static void copy(byte[] bytes,OutputStream output) throws IOException {
        try {
            output.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            output.flush();
            output.close();
        }
    }
    public static void copyFile(InputStream input,String filePath) throws IOException {
        OutputStream out = null;
        try {
            File file = new File(filePath);
            if (!file.exists()){
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            out = new FileOutputStream(file);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                out.write(buf, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            input.close();
            out.flush();
            out.close();
        }
    }


}
